package com.kecheng.com.yx.restful;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.kecheng.util.CreateLinkErweima;
import com.kecheng.util.SpringUtil;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;


public class TbOrderRest extends HttpServlet{
	private static final long serialVersionUID = 4440739483644821986L; 
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        response.setCharacterEncoding("UTF-8"); 
        String jsonData = request.getParameter("tbOrders");
        //System.out.println("String:"+jsonData);
		JSONArray dataJson = JSONArray.fromObject(jsonData);
		//System.out.println("dataJson:"+dataJson);
		
//		List<TbOrder> tbOrders = (List<TbOrder>)JSONArray.toList(dataJson, TbOrder.class);
//		if(jsonData!=null){
//			for(int i=0;i<tbOrders.size();i++){
//			}
//		}
		String msg = "";
		if(jsonData!=null){
			msg="restful对接成功！";
		}else{
			msg="restful没数据呀！";
		}
		 PrintWriter out;
		try {
			out = response.getWriter();
			out.print(msg);  
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
	
	/**
	 * 根技pid生成二维码并返回
	 * 
	 * @param pid
	 * @return
	 * @throws IOException
	 */
	private String makeerweima(Integer pid) throws IOException {
		// 生成养鸡场二维码
		String text = "http://nong.xmyunxin.com/w/product!jichangzuizhong.com.kecheng.action?product.id="
				+ pid;
		int width = 300;
		int height = 300;
		// 二维码的图片格式
		String format = "jpg";
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		// 内容所使用编码
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = null;
		try {
			bitMatrix = new MultiFormatWriter().encode(text,
					BarcodeFormat.QR_CODE, width, height, hints);
		} catch (WriterException e1) {
			// TODO Auto-generated catch block
			System.out.println("出错了");
			e1.printStackTrace();
		}
		// 生成二维码
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		File outputFile = new File(root + File.separator
				+ System.currentTimeMillis() + ".jpg");
		CreateLinkErweima.writeToFile(bitMatrix, format, outputFile);
		System.out.println(outputFile.getName());
		return "upload" + File.separator + outputFile.getName();
	}
	
	
	/**
	 * 请求post
	 * @param
	 */
	public static void httpUrlConn(){
		try {
			String resurl = "http://h5.jimugame.com/List/apiGameList";
			URL url = new URL(resurl);  
			HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();  
			
			httpUrlConn.setDoOutput(true);  
			httpUrlConn.setDoInput(true);  
			httpUrlConn.setUseCaches(false);  
			// 
			InputStream inputStream = httpUrlConn.getInputStream();  
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
			StringBuffer buffer = new StringBuffer();  
			String str = null;  
			while ((str = bufferedReader.readLine()) != null) {  
				buffer.append(str);  
			}  
			bufferedReader.close();  
			inputStreamReader.close();  
			// 
			inputStream.close();  
			System.out.println("result:"+buffer.toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
//      JSONArray jsonArray = JSONArray.fromObject(buffer.toString());
//      gameQuerys= JSONArray.toList(jsonArray, GameQuery.class);

	}
	
	public static void main(String[] args){
		String aa = "[{'price':999,'status':'订单成功0','oid':'3574635346389'},{'price':99,'status':'订单成功','oid':'3574635346387'}]";
		JSONArray jsonArray = JSONArray.fromObject(aa);
//		List<TbOrder> tbOrders = (List<TbOrder>)JSONArray.toList(jsonArray, TbOrder.class);
//		for(int i=0;i<tbOrders.size();i++){
//			System.out.println(tbOrders.get(i).getStatus());
//			System.out.println(tbOrders.get(i).getOid());
//			System.out.println(tbOrders.get(i).getPrice());
//		}
	}
	
}
